<template>
  <div class="brand">
    <div class="brand_top">
      <el-input v-model="listQuery.id" placeholder="请输入品牌制造商ID" />
      <el-input v-model="listQuery.name" placeholder="请输入品牌制造商名称" />
      <el-button type="primary" icon="el-icon-search" @click="getBrandList">查找</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="addBrand">添加</el-button>
      <el-button type="primary" icon="el-icon-download" @click="exportBrandList">导出</el-button>
    </div>
    <div class="brand_bottom">
      <el-table v-loading="listLoading" :data="list" element-loading-text="正在查询中。。。" fit highlight-current-row border>
        <el-table-column label="品牌商ID" width="97">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌商名称" width="94">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌商图片" width="94">
          <template slot-scope="scope">
            <span>
              <el-image :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]" alt="" class="picUrl" :headers="headers" />
            </span>
          </template>
        </el-table-column>
        <el-table-column label="介绍" width="473">
          <template slot-scope="scope">
            <span>{{ scope.row.desc }}</span>
          </template>
        </el-table-column>
        <el-table-column label="底价" width="94">
          <template slot-scope="scope">
            <span>{{ scope.row.floorPrice }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" min-width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="editBrand(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delBrandList(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>
    <div class="brand_dialog">
      <!-- 添加模态框 -->
      <el-dialog title="创建" :visible.sync="dialogAddVisible">
        <el-form ref="ruleForm" :model="dialogAddData" :rules="rules">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="dialogAddData.name" />
          </el-form-item>
          <el-form-item label="介绍" prop="desc">
            <el-input v-model="dialogAddData.desc" />
          </el-form-item>
          <el-form-item label="商品图片" prop="imageUrl">
            <!-- :before-upload="beforeAvatarUpload" -->
            <el-upload
              class="avatar-uploader"
              :action="imageUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :headers="headers"
            >
              <el-image v-if="dialogAddData.imageUrl" :src="dialogAddData.imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="底价" prop="floorPrice">
            <el-input v-model="dialogAddData.floorPrice" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBrandList('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑模态框 -->
      <el-dialog title="编辑" :visible.sync="dialogEditVisible">
        <el-form ref="ruleForm" :model="dialogEditData" :rules="rules">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="dialogEditData.name" />
          </el-form-item>
          <el-form-item label="介绍" prop="desc">
            <el-input v-model="dialogEditData.desc" />
          </el-form-item>
          <el-form-item label="商品图片" prop="imageUrl">
            <!-- :before-upload="beforeAvatarUpload" -->
            <el-upload
              class="avatar-uploader"
              :action="imageUrl"
              :headers="headers"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
            >
              <img v-if="dialogEditData.imageUrl" :src="dialogEditData.imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
          <el-form-item label="底价" prop="floorPrice">
            <el-input v-model="dialogEditData.floorPrice" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEditVisible = false">取 消</el-button>
          <el-button type="primary" @click="trueEditBrandList('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { listBrand, createBrand, deleteBrand, updateBrand } from '@/api/brand'
import Pagination from '@/components/Pagination/index.vue'
import { downloadXlsx } from '@/utils/xlsx'
import { getToken } from '@/utils/auth'
import { uploadPath } from '@/api/storage'

export default {
  components: {
    Pagination
  },
  data() {
    return {
      brand_ruleForm: {
        brandId: '',
        brandName: ''
      },
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        id: undefined,
        status: '',
        sort: 'add_time',
        order: 'desc'
      },
      // 获取的品牌制造商列表总条数
      total: 0,
      // 获取的品牌制造商列表数据
      list: [],
      listLoading: true,
      dialogAddVisible: false,
      dialogEditVisible: false,
      dialogAddData: {
        name: '',
        desc: '',
        imageUrl: '',
        floorPrice: ''
      },
      dialogEditData: {
        name: '',
        desc: '',
        imageUrl: '',
        floorPrice: ''
      },
      dialogTitle: '',
      rules: {
        name: [
          { required: true, message: '品牌商名称不能为空', trigger: 'blur' }
        ]
      },
      headers: {},
      imageUrl: '',
      listLoading: true
    }
  },
  mounted() {
    this.getBrandList()
    this.headers = { 'X-Litemall-Admin-Token': getToken() }
    this.imageUrl = uploadPath
  },
  methods: {
    getList() {
      this.listLoading = true
      listBrand(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false
        })
        .catch(() => {
          this.list = []
          this.total = 0
        })
    },
    // 获取品牌列表
    async getBrandList() {
      const res = await listBrand({
        page: this.listQuery.page,
        limit: this.listQuery.limit,
        name: this.listQuery.name,
        id: this.listQuery.id,
        sort: this.listQuery.sort,
        order: this.listQuery.order
      })
      console.log(res.data.data, 'res')
      this.list = res.data.data.list
      console.log(this.list, 'list')
      this.total = res.data.data.total
      this.listLoading = false
    },
    // 添加品牌
    addBrand() {
      this.dialogAddVisible = true
      this.dialogAddData.imageUrl = ''
      this.dialogAddData.floorPrice = ''
      this.dialogAddData.desc = ''
      this.dialogAddData.name = ''
    },
    // 真正的添加品牌
    addBrandList(formName) {
      console.log(typeof this.dialogAddData.floorPrice, 'aaa')

      if (this.dialogAddData.floorPrice == '' && this.dialogAddData.desc == '') {
        this.$notify.error({
          title: '失败',
          message: '参数值不对'
        })
      } else {
        this.$refs[formName].validate(async(valid) => {
          if (valid) {
            console.log(valid)
            const res = await createBrand({
              desc: this.dialogAddData.desc,
              floorPrice: this.dialogAddData.floorPrice,
              name: this.dialogAddData.name,
              picUrl: this.dialogAddData.imageUrl
            })
            console.log(res, 'res')

            if (res.data.errmsg == '成功') {
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success'
              })
              this.dialogAddVisible = false
              this.getBrandList()
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
        this.dialogVisible = false
      }
    },
    // 导出品牌信息
    exportBrandList() {
      const dataList = []
      this.list.forEach((item, index) => {
        const arr = []
        arr[0] = item.id
        arr[1] = item.name
        arr[2] = item.desc
        dataList.push(arr)
      })
      dataList.unshift(['品牌商ID', '品牌商名称', '简介'])

      downloadXlsx(dataList, '品牌商信息.xlsx')
    },
    // 编辑品牌信息
    editBrand(row) {
      console.log(row, 'ASDF')

      this.dialogEditVisible = true
      this.dialogEditData = row
      this.dialogEditData.name = row.name
      this.dialogEditData.desc = row.desc
      this.dialogEditData.floorPrice = row.floorPrice
      this.dialogEditData.imageUrl = row.picUrl
      this.dialogEditData.picUrl = row.picUrl
    },
    // 真正的编辑品牌信息
    async trueEditBrandList() {
      this.dialogEditData.picUrl = this.dialogEditData.imageUrl
      console.log(this.dialogEditData, 'aaaASD')
      const res = await updateBrand(this.dialogEditData)
      if (res) {
        this.$notify({
          title: '成功',
          message: '编辑成功',
          type: 'success'
        })
        this.dialogEditVisible = false
        this.getBrandList()
      }
    },
    // 删除品牌信息
    async delBrandList(row) {
      const res = await deleteBrand({
        addTime: row.addTime,
        deleted: row.deleted,
        desc: row.desc,
        floorPrice: row.floorPrice,
        id: row.id,
        name: row.name,
        picUrl: 'blob:' + row.picUrl,
        preview: ['blob:' + row.picUrl],
        sortOrder: row.sortOrder,
        updateTime: row.updateTime
      })
      console.log(res, 'resDelete')
      if (res.status == 200) {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
        this.getBrandList()
      }
    },
    // 添加模态框上传图片
    handleAvatarSuccess(res, file) {
      console.log(file, 'file')
      this.dialogAddData.imageUrl = file.response.data.url
      this.dialogEditData.imageUrl = file.response.data.url
    }
  }
}
</script>
<style lang="scss" scoped>
.brand {
  padding: 20px;

  .brand_top {
    display: flex;
    margin-bottom: 20px;

    ::v-deep .el-input--mini {
      width: 200px !important;

      .el-input__inner {
        width: 100%;
      }
    }
  }

  .brand_bottom {
    ::v-deep .el-table__cell {
      .cell {
        text-align: center;
      }
    }
  }

  .el-dialog {
    .el-dialog__body {
      padding: 30px 20px;

      .el-form {
        margin-left: 50px;

        .el-form-item {
          display: flex;

          ::v-deep .el-form-item__label {
            width: 80px !important;
          }

          ::v-deep .el-input {
            width: 300px !important;

            .el-input__inner {
              width: 100%;
            }
          }

          .avatar-uploader {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
          }

          .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
          }

          .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
          }

          .avatar {
            width: 178px;
            height: 178px;
            display: block;
          }
        }
      }
    }

  }
}

.picUrl {
  width: 40px;
  height: 40px;
}
</style>
<template>
  <div class="brand">
    <div class="brand_top">
      <el-input v-model="listQuery.id" placeholder="请输入品牌制造商ID" />
      <el-input v-model="listQuery.name" placeholder="请输入品牌制造商名称" />
      <el-button type="primary" icon="el-icon-search" @click="getBrandList">查找</el-button>
      <el-button type="primary" icon="el-icon-edit" @click="addBrand">添加</el-button>
      <el-button type="primary" icon="el-icon-download" @click="exportBrandList">导出</el-button>
    </div>
    <div class="brand_bottom">
      <el-table :data="list" element-loading-text="正在查询中。。。" fit highlight-current-row border v-loading="listLoading">
        <el-table-column label="品牌商ID" width="97">
          <template slot-scope="scope">
            <span>{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌商名称" width="94">
          <template slot-scope="scope">
            <span>{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌商图片" width="94">
          <template slot-scope="scope">
            <span>
              <el-image :src="scope.row.picUrl" :preview-src-list="[scope.row.picUrl]" alt="" class="picUrl" :headers="headers"></el-image>
            </span>
          </template>
        </el-table-column>
        <el-table-column label="介绍" width="473">
          <template slot-scope="scope">
            <span>{{ scope.row.desc }}</span>
          </template>
        </el-table-column>
        <el-table-column label="底价" width="94">
          <template slot-scope="scope">
            <span>{{ scope.row.floorPrice }}</span>
          </template>
        </el-table-column>
        <el-table-column align="center" label="操作" min-width="200" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button type="primary" size="mini" @click="editBrand(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="delBrandList(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit"
        @pagination="getList" />
    </div>
    <div class="brand_dialog">
      <!-- 添加模态框 -->
      <el-dialog title="创建" :visible.sync="dialogAddVisible">
        <el-form ref="ruleForm" :model="dialogAddData" :rules="rules">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="dialogAddData.name" />
          </el-form-item>
          <el-form-item label="介绍" prop="desc">
            <el-input v-model="dialogAddData.desc" />
          </el-form-item>
          <el-form-item label="商品图片" prop="imageUrl">
            <!-- :before-upload="beforeAvatarUpload" -->
            <el-upload class="avatar-uploader" :action="imageUrl" :show-file-list="false"
              :on-success="handleAvatarSuccess" :headers="headers">
              <el-image v-if="dialogAddData.imageUrl" :src="dialogAddData.imageUrl" class="avatar"></el-image>
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="底价" prop="floorPrice">
            <el-input v-model="dialogAddData.floorPrice" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogAddVisible = false">取 消</el-button>
          <el-button type="primary" @click="addBrandList('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 编辑模态框 -->
      <el-dialog title="编辑" :visible.sync="dialogEditVisible">
        <el-form ref="ruleForm" :model="dialogEditData" :rules="rules">
          <el-form-item label="品牌名称" prop="name">
            <el-input v-model="dialogEditData.name" />
          </el-form-item>
          <el-form-item label="介绍" prop="desc">
            <el-input v-model="dialogEditData.desc" />
          </el-form-item>
          <el-form-item label="商品图片" prop="imageUrl">
            <!-- :before-upload="beforeAvatarUpload" -->
            <el-upload class="avatar-uploader" :action="imageUrl"  :headers="headers"
              :show-file-list="false" :on-success="handleAvatarSuccess">
              <img v-if="dialogEditData.imageUrl" :src="dialogEditData.imageUrl" class="avatar">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item label="底价" prop="floorPrice">
            <el-input v-model="dialogEditData.floorPrice" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogEditVisible = false">取 消</el-button>
          <el-button type="primary" @click="trueEditBrandList('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { listBrand, createBrand, deleteBrand, updateBrand } from '@/api/brand'
import Pagination from '@/components/Pagination/index.vue'
import { downloadXlsx } from '@/utils/xlsx';
import { getToken } from '@/utils/auth'
import { uploadPath } from '@/api/storage'

export default {
  components: {
    Pagination
  },
  data() {
    return {
      brand_ruleForm: {
        brandId: '',
        brandName: ''
      },
      listQuery: {
        page: 1,
        limit: 20,
        name: undefined,
        id: undefined,
        status: '',
        sort: 'add_time',
        order: 'desc'
      },
      // 获取的品牌制造商列表总条数
      total: 0,
      // 获取的品牌制造商列表数据
      list: [],
      listLoading: true,
      dialogAddVisible: false,
      dialogEditVisible: false,
      dialogAddData: {
        name: '',
        desc: '',
        imageUrl: '',
        floorPrice: ''
      },
      dialogEditData: {
        name: '',
        desc: '',
        imageUrl: '',
        floorPrice: ''
      },
      dialogTitle: '',
      rules: {
        name: [
          { required: true, message: '品牌商名称不能为空', trigger: 'blur' }
        ]
      },
      headers: {},
      imageUrl: '',
      listLoading: true
    }
  },
  mounted() {
    this.getBrandList()
    this.headers = { 'X-Litemall-Admin-Token': getToken() }
    this.imageUrl = uploadPath
  },
  methods: {
    getList() {
      this.listLoading = true
      listBrand(this.listQuery)
        .then(response => {
          this.list = response.data.data.list
          this.total = response.data.data.total
          this.listLoading = false;
        })
        .catch(() => {
          this.list = []
          this.total = 0
        })
    },
    // 获取品牌列表
    async getBrandList() {
      const res = await listBrand({
        page: this.listQuery.page,
        limit: this.listQuery.limit,
        name: this.listQuery.name,
        id: this.listQuery.id,
        sort: this.listQuery.sort,
        order: this.listQuery.order
      })
      console.log(res.data.data, 'res')
      this.list = res.data.data.list
      console.log(this.list, 'list')
      this.total = res.data.data.total
      this.listLoading = false
    },
    // 添加品牌
    addBrand(){
      this.dialogAddVisible = true;
      this.dialogAddData.imageUrl = '';
      this.dialogAddData.floorPrice = '';
      this.dialogAddData.desc = '';
      this.dialogAddData.name = '';
    },
    // 真正的添加品牌
    addBrandList(formName) {
      console.log(typeof this.dialogAddData.floorPrice, 'aaa');

      if (this.dialogAddData.floorPrice == '' && this.dialogAddData.desc == '') {
        this.$notify.error({
          title: '失败',
          message: '参数值不对'
        })
      } else {
        this.$refs[formName].validate(async (valid) => {
          if (valid) {
            console.log(valid)
            const res = await createBrand({
              desc: this.dialogAddData.desc,
              floorPrice: this.dialogAddData.floorPrice,
              name: this.dialogAddData.name,
              picUrl: this.dialogAddData.imageUrl
            })
            console.log(res, 'res')

            if (res.data.errmsg == '成功') {
              this.$notify({
                title: '成功',
                message: '创建成功',
                type: 'success'
              })
              this.dialogAddVisible = false
              this.getBrandList()
            }
          } else {
            console.log('error submit!!')
            return false
          }
        })
        this.dialogVisible = false;
      }
    },
    // 导出品牌信息
    exportBrandList() {
      let dataList = []
      this.list.forEach((item, index) => {
        let arr = [];
        arr[0] = item.id;
        arr[1] = item.name;
        arr[2] = item.desc;
        dataList.push(arr)
      })
      dataList.unshift(['品牌商ID', '品牌商名称', '简介'])

      downloadXlsx(dataList, '品牌商信息.xlsx')
    },
    // 编辑品牌信息
    editBrand(row) {
      console.log(row,'ASDF');

      this.dialogEditVisible = true
      this.dialogEditData = row
      this.dialogEditData.name = row.name
      this.dialogEditData.desc = row.desc
      this.dialogEditData.floorPrice = row.floorPrice
      this.dialogEditData.imageUrl = row.picUrl
      this.dialogEditData.picUrl = row.picUrl

    },
    // 真正的编辑品牌信息
    async trueEditBrandList() {
      this.dialogEditData.picUrl = this.dialogEditData.imageUrl
      console.log(this.dialogEditData, 'aaaASD')
      const res = await updateBrand(this.dialogEditData)
      if (res) {
        this.$notify({
          title: '成功',
          message: '编辑成功',
          type: 'success'
        })
        this.dialogEditVisible = false
        this.getBrandList()
      }
    },
    // 删除品牌信息
    async delBrandList(row) {
      const res = await deleteBrand({
        addTime: row.addTime,
        deleted: row.deleted,
        desc: row.desc,
        floorPrice: row.floorPrice,
        id: row.id,
        name: row.name,
        picUrl: 'blob:' + row.picUrl,
        preview: ['blob:' + row.picUrl],
        sortOrder: row.sortOrder,
        updateTime: row.updateTime
      })
      console.log(res, 'resDelete')
      if (res.status == 200) {
        this.$notify({
          title: '成功',
          message: '删除成功',
          type: 'success'
        })
        this.getBrandList()
      }
    },
    // 添加模态框上传图片
    handleAvatarSuccess(res, file) {
      console.log(file, 'file');
      this.dialogAddData.imageUrl = file.response.data.url
      this.dialogEditData.imageUrl = file.response.data.url
    }
  }
}
</script>
<style lang="scss" scoped>
.brand {
  padding: 20px;

  .brand_top {
    display: flex;
    margin-bottom: 20px;

    ::v-deep .el-input--mini {
      width: 200px !important;

      .el-input__inner {
        width: 100%;
      }
    }
  }

  .brand_bottom {
    ::v-deep .el-table__cell {
      .cell {
        text-align: center;
      }
    }
  }

  .el-dialog {
    .el-dialog__body {
      padding: 30px 20px;

      .el-form {
        margin-left: 50px;

        .el-form-item {
          display: flex;

          ::v-deep .el-form-item__label {
            width: 80px !important;
          }

          ::v-deep .el-input {
            width: 300px !important;

            .el-input__inner {
              width: 100%;
            }
          }

          .avatar-uploader {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
          }

          .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
          }

          .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
          }

          .avatar {
            width: 178px;
            height: 178px;
            display: block;
          }
        }
      }
    }

  }
}

.picUrl {
  width: 40px;
  height: 40px;
}
</style>
